//index.js
//获取应用实例
const app = getApp()
// 引入api文件  直接找里面的default
const api = require("../../utils/api.js").default;
// 引入封装好的请求方式
const myRequest = require("../../utils/request.js").default;


Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    indicatorDots: true,
    autoplay:true
  },
  toDetails(e){
    console.log(e.currentTarget.dataset);
    var shopId = e.currentTarget.dataset.shopid;
    wx.navigateTo({
      url: '/pages/details/details?id=' + shopId
    })
  },
  toTopicDetails(e) {
    console.log(e.currentTarget.dataset);
    var topicId = e.currentTarget.dataset.topicid;
    wx.navigateTo({
      url: '/pages/topicDetails/topicDetails?id=' + topicId
    })
  },
  toBrandDetail(e) {
    console.log(e.currentTarget.dataset);
    var brandid = e.currentTarget.dataset.brandid;
    wx.navigateTo({
      url: '/pages/brandDetail/brandDetail?id=' + brandid
    })
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
        // 登录商城
        this.loginShop();
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  onShow:function(){
    var self = this
    wx.showLoading({
      title: '正在加载中喔',
      success:function(){
        wx.request({
          url: api.index,
          success: function (res) {
            console.log("首页数据", res.data)
            self.setData({
              // 导航数据
              banner: res.data.data.banner,
              // 品牌列表
              brandList: res.data.data.brandList,
              // 新品
              newGoodsList: res.data.data.newGoodsList,
              // 人气推荐
              hotGoodsList: res.data.data.hotGoodsList,

            })

          }
        });
        wx.request({
          url: api.topList,
          success: function (res) {
            console.log("专题列表", res.data.data.data);
            self.setData({
              topList: res.data.data.data
            })
            console.log("专题精选", self.data.topList);
            wx.hideLoading();
          }
        })
      }
    })
   
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail
    this.setData({
      userInfo: e.detail,
      hasUserInfo: true
    })
  },
  loginShop(){
    wx.request({
      url: api.loginByWeixin,
      method: "POST",
      data:{
        appId: "wx24e6b24f386db973",
        secret: "e454b688d3884849dd91c59bd619f71e",
        code: app.globalData.code,
        userInfo: app.globalData.userInfo
      },
      success(res){
        wx.setStorage({
          key: 'token',
          data: res.data.data.token,
          success: function () {
            // console.log("已缓存token");
            myRequest(api.index, 'GET', {}, function () {
              console.log("成功回调",res)
            });
          }
        })
      }
    })
    
  }
})
